package com.smart.manager.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ExceptionTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.manager.system.entities.body.RSystemMenu;
import com.smart.manager.system.entities.models.SystemMenu;
import com.smart.manager.system.repository.MenuRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 系统菜单- 前端API菜单
 * 当前页面的菜单需要超级管理员的角色才能登录
 *
 * @author xiaxuqiu
 * @date 2022/11/18
 */
@Api(tags =  "菜单管理")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "system/menu")
@RequiredArgsConstructor
public class MenuApi {

    private final MenuRepository menuRepository;


    @GetMapping("list")
    @ApiOperation(value = "查询菜单")
    @SaCheckPermission("system:menu:list")
    public AjaxResult<List<SystemMenu>> getList() {
        List<SystemMenu> result = menuRepository.getFullList();
        return AjaxResult.data( result );
    }


    @ApiOperation(value = "添加菜单")
    @PostMapping("add")
    @SaCheckPermission("system:menu:add")
    public AjaxResult<String> add(@RequestBody @Validated RSystemMenu.EditBody data) {
        menuRepository.add( data );
        return AjaxResult.success();
    }


    @ApiOperation(value = "修改菜单")
    @PutMapping("update/{id}")
    @SaCheckPermission("system:menu:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RSystemMenu.EditBody data) {
        menuRepository.update( id, data );
        return AjaxResult.success();
    }


    @ApiOperation(value = "删除菜单")
    @DeleteMapping("delete")
    @SaCheckPermission("system:menu:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ExceptionTips.PK_EMPTY) List<Long> ids) {
        int i = menuRepository.deleteByIds( ids );
        return AjaxResult.delSuccess( i );
    }

}
